#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int n = fruits.size();
        map<int, int> mp;
        int len = 0;
        for (int right = 0, left = 0; right < n; right++)
        {
            mp[fruits[right]]++;
            if (mp.size() > 2)
            {
                mp[fruits[left]]--;
                if (mp[fruits[left]] == 0)
                    mp.erase(fruits[left]);
                left++;
            }
            len = max(len, right - left + 1);
        }
        return len;
    }
};